<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
      @reset="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="平台名称" name="name">
            <t-input v-model="queryParams.name" clearable placeholder="请输入平台名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="平台类别" name="platformCategory">
            <t-select style="width: 240px" clearable v-model="queryParams.platformCategory"
              :options="dict.type.platform_category">
            </t-select>
          </t-form-item>
          <t-form-item label="平台级别" name="platformLevel">
            <t-select style="width: 240px" clearable v-model="queryParams.platformLevel"
              :options="dict.type.platform_level">
            </t-select>
          </t-form-item>
          <t-form-item label="平台研究方向" name="researchDirection">
            <t-select style="width: 240px" clearable v-model="queryParams.researchDirection"
              :options="dict.type.research_direction">
            </t-select>
          </t-form-item>
          <t-form-item label="平台状态" name="platformStatus">
            <t-select style="width: 240px" clearable v-model="queryParams.platformStatus"
              :options="dict.type.platform_status">
            </t-select>
          </t-form-item>
          <t-form-item label="建设单位" name="unitName">
            <t-input v-model="queryParams.unitName" clearable placeholder="请输入建设单位" style="width: 240px" />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="base" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table rowKey="index" size="small" :data="data" :columns="columns" hover :pagination="pagination"
      :loading="dataLoading" :sort="sort" :hideSortTips="true" @sort-change="sortChange">
      <template #name="{ row }">
        <t-link @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_project_detail = true"
          hover="color" theme="primary">{{ row.name }}</t-link>
      </template>
      <template #platformCategory="{ row }">
        {{ row.platformCategory | filterByDict(dict.type.platform_category) }}
      </template>
      <template #platformLevel="{ row }">
        {{ row.platformLevel | filterByDict(dict.type.platform_level) }}
      </template>
      <template #researchDirection="{ row }">
        {{ row.researchDirection | filterByDict(dict.type.research_direction) }}
      </template>
      <template #platformStatus="{ row }">
        <t-tag class="cursor-pointer" :theme="handleTheme(row.platformStatus, dict.type.platform_status)"
          variant="light">
          {{ row.platformStatus | filterByDict(dict.type.platform_status) }}
        </t-tag>
      </template>
      <template #address="{ row }">
        {{ row.address || "-" }}
      </template>
      <template #postCode="{ row }">
        {{ row.postCode || "-" }}
      </template>
      <template #website="{ row }">
        {{ row.website || "-" }}
      </template>
      <template #content="{ row }">
        {{ row.content || "-" }}
      </template>
      <template #zpbg="{ row }">
        <t-link v-for="file in row.fileLists" theme="primary" hover="color" :key="file.id" :href="file.url"
          target="_blank">{{ file.name }}</t-link>
      </template>
      <template #op="{ row }">
        <!-- <t-button
          @click="edit(row)"
          size="small"
          variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          编辑
        </t-button> -->
        <file-upload v-show="false" @success="uploadSuccess" v-model="row.fileIds" moduleKey="kjptkhpd"
          :multiple="true">
          <button ref="upload"></button>
        </file-upload>
        <t-button size="small" variant="text" class="t-button-link"
          @click="objId = row.id; currentRow = row; $refs.upload.click()">
          <t-icon name="cloud-upload" slot="icon" />
          上传考核评定
        </t-button>
        <t-button size="small"
          @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_project_detail = true" variant="text"
          class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
      </template>
    </t-table>
    <platform-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail"
      v-if="dialog.open_project_detail"></platform-detail-dialog>
  </div>
</template>

<script>
import PlatformDetailDialog from "./components/platform-detail-dialog.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import { getFiles } from "@/api/common";

export default {
  name: "platform-index",
  dicts: ["platform_category", "platform_status", 'platform_level', 'research_direction'],
  components: {
    PlatformDetailDialog,
    FileUpload
  },
  data () {
    return {
      sort: [],
      dialog: {
        open_project_detail: false,
        header: "",
        objId: 0,
      },
      dictTypes: {},
      projectCategoryOptions: [],
      columns: [
        {
          colKey: "name",
          title: "平台名称",
          align: "center",
          width: 220

        },
        {
          colKey: "platformCategory",
          title: "平台类别",
          align: "center",
          width: 160,
        },
        {
          colKey: "unitName",
          title: "建设单位",
          align: "center",
          width: 160,
        },
        {
          colKey: "platformLevel",
          title: "平台级别",
          align: "center",
          width: 160,
        },
        {
          colKey: "researchDirection",
          title: "研究方向",
          align: "center",
          width: 160,
        },
        // {
        //   colKey: "unitName",
        //   title: "建设单位",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "address",
        //   title: "地址",
        //   align: "center",
        //   width: 160,
        // },
        // {
        //   colKey: "postCode",
        //   title: "邮编",
        //   align: "center",
        //   width: 100,
        // },
        {
          colKey: "buildDate",
          title: "立项时间",
          align: "center",
          sorter: true,
          width: 120,
        },
        {
          colKey: "website",
          title: "网址",
          align: "center",
          width: 100,
        },
        // {
        //   colKey: "content",
        //   title: "平台建设内容",
        //   align: "center",
        //   width: 180
        // },
        {
          colKey: "platformStatus",
          title: "平台状态",
          align: "center",
          width: 180,
          fixed: 'right'
        },
        {
          colKey: "zpbg",
          title: "考核评定",
          align: "center",
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 220,
          fixed: 'right'
        },
      ],
      data: [
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        platformCategory: "",
        platformLevel: '',
        researchDirection: '',
        platformStatus: '',
        unitName: ""
      },
      objId: 0,
      currentRow: {},
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  watch: {
    data: {
      handler (val) {
        val.forEach(row => {
          getFiles(row.id).then((res) => {
            const files = []
            res.data.forEach((item) => {
              files.push({
                name: item.fileName,
                url: item.fullFilePath,
                id: item.id,
                filePath: item.filePath
              })
            })
            row.fileLists = files
            this.$forceUpdate()
          }).catch((e) => {
            this.$message.error(e.toString());
          });
        })
      },
      immediate: true
    }
  },
  async created () {
    Object.assign(this.queryParams, this.handelQuery(this.queryParams, this.$route.query))
    this.getList();
  },
  methods: {
    uploadSuccess (ids) {
      this.$api.kjpt.project.reportEdit({
        id: this.objId,
        ...this.currentRow,
        sysAttMainVo: {
          sysAttMainIds: ids,
        }
      }).then(val => {
        this.getList()
        this.$message.success("上传成功");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    // 编辑按钮
    edit (row) {
      this.$router.push({ path: `/kjpt/other/platform/platform-apply/${row.id}`, query: { title: "平台编辑" } });
    },
    createPDF (row) {
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.kjxm.project.projectPDF(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    sortChange (sort) {
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if (sort instanceof Array) {
        orderByColumn = sort.map(val => val.sortBy)
        isAsc = sort.map(val => val.descending ? `desc` : `asc`)
      } else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending ? `desc` : `asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    getList () {
      this.dataLoading = true;
      this.queryParams.pageNum = this.pagination.current || 1;
      this.$api.kjpt.project.platformReportList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },

  }
}
</script>

<style scoped></style>
